/***
 * Copyright (C) 2011  wexoo
 * p.weixlbaumer@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.wexoo.group_cost_calculator.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * DALUtils.java
 * 
 * @author wexoo
 * @version Aug 26, 2011
 */
public class DALUtils {

   public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
   public static final SimpleDateFormat DEFAULT_DATE_FORMATTER = new SimpleDateFormat(DEFAULT_DATE_FORMAT);

   public static final String GERMAN_DATE_FORMAT = "dd.MM.yyyy";
   public static final SimpleDateFormat GERMAN_DATE_FORMATTER = new SimpleDateFormat(GERMAN_DATE_FORMAT);

   public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
   public static final SimpleDateFormat DEFAULT_TIME_FORMATTER = new SimpleDateFormat(DEFAULT_TIME_FORMAT);

   public static final String DEFAULT_TIME_FORMAT_WITHOUT_SECONDS = "HH:mm";
   public static final SimpleDateFormat DEFAULT_TIME_FORMATTER_WITHOUT_SECONDS =
         new SimpleDateFormat(DEFAULT_TIME_FORMAT_WITHOUT_SECONDS);

   /**
    * Returns the long value of a string.
    * 
    * @param value the string to convert in long
    * @return the value as long
    * @author wexoo
    * @since 1.0.0 Jul 6, 2011
    * @version 1.0.0 Jul 6, 2011
    */
   public static Long getLongValueOfString(final String value) {
      try {
         return value.equals("") ? null : Long.valueOf(value.replaceAll("[^\\d]", "").replaceAll(" ", ""));
      } catch (final NumberFormatException e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * Returns the integer value of a string.
    * 
    * @param value the string to convert in integer
    * @return the value as integer
    * @author wexoo
    * @since 1.0.0 Jul 6, 2011
    * @version 1.0.0 Jul 6, 2011
    */
   public static Integer getIntegerValueOfString(final String value) {
      if (value != "null") {
         try {
            return value.equals("") ? null : Integer.valueOf(value);
         } catch (final Exception e) {
            e.printStackTrace();
         }
      }
      return null;
   }

   /**
    * Returns the double value of a string.
    * 
    * @param value the string to convert in double
    * @return the value as double
    * @author wexoo
    * @since 1.0.0 Jul 6, 2011
    * @version 1.0.0 Jul 6, 2011
    */
   public static Double getDoubleValueOfString(final String value) {
      try {
         return value.equals("") ? null : Double.valueOf(value);
      } catch (final NumberFormatException e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * Returns the default date format of a string.
    * 
    * @see net.ausgstecktis.annotation.Settings#defaultDateFormat()
    * @param value the string to convert in a specific date format
    * @return the date in format
    * @author wexoo
    */
   public static Date getDefaultDateValueOfString(final String value) {
      try {
         return value.equals("") ? null : DEFAULT_DATE_FORMATTER.parse(value);
      } catch (final ParseException e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * Returns the default date format of a date.
    * 
    * @see net.ausgstecktis.annotation.Settings#defaultDateFormat()
    * @param value the string to convert in a specific date format
    * @return the date in format
    * @author wexoo
    */
   public static String getStringValueOfDefaultDate(final Date value) {
      return value == null ? null : DEFAULT_DATE_FORMATTER.format(value);
   }

   /**
    * Returns the default time format of a string.
    * 
    * @see net.ausgstecktis.annotation.Settings#defaultTimeFormat()
    * @param value the value
    * @return the time format in {}
    * @author wexoo
    */
   public static Date getTimeValueOfString(final String value) {
      try {
         return value.equals("") ? null : DEFAULT_TIME_FORMATTER.parse(value);
      } catch (final ParseException e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * Get the database version from API.
    * 
    * @param appVersionName the version name from this app
    * @param apiLevel the api level
    * @param apiVersion the api version
    * @return the db version as String
    * @author wexoo
    * @since 1.0.0 Jul 6, 2011
    * @version 1.0.0 Jul 6, 2011
    */
   //   public static String getOnlineDatabaseVersion(final String appVersionName, final int apiLevel,
   //         final String apiVersion) {
   //      String dbVersion = Preferences.getStringPreference(R.string.local_database_version_key, null);
   //
   //      final RestClient ri = new RestClient(HeurigenApp.getConfig().apiUri(), new String("version/current/dbversion/"
   //            + dbVersion + "/appversion/" + appVersionName + "/device/android/apiversion/" + apiVersion
   //            + "/apilevel/" + apiLevel));
   //      try {
   //         ri.callWebService(RestClient.GET);
   //      } catch (final IllegalStateException e) {
   //         e.printStackTrace();
   //      } catch (final ClientProtocolException e) {
   //         e.printStackTrace();
   //      } catch (final IOException e) {
   //         e.printStackTrace();
   //      }
   //
   //      try {
   //         if (ri.getObjectJson() != null) {
   //            final JSONArray jArray = ri.getObjectJson();
   //            for (int i = 0; i < jArray.length(); i++) {
   //               final JSONObject json_data = jArray.getJSONObject(i);
   //               dbVersion = json_data.getString("db_version");
   //            }
   //         }
   //      } catch (final JSONException e) {
   //         e.printStackTrace();
   //      }
   //      return dbVersion;
   //   }

}